package com.cuihanyu.newuniversityover.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.cuihanyu.newuniversityover.R;
import com.cuihanyu.newuniversityover.adapter.HomeExpandableListViewAdapter;
import com.cuihanyu.newuniversityover.adapter.HomeRecyclerviewAdapter;
import com.cuihanyu.newuniversityover.bean.HomeEXDetailBean;
import com.cuihanyu.newuniversityover.internet.GlobalContent;
import com.cuihanyu.newuniversityover.present.MVPPresent;
import com.cuihanyu.newuniversityover.util.GsonUtil;
import com.cuihanyu.newuniversityover.util.MyLogUtil;
import com.cuihanyu.newuniversityover.view.MVPView;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * Created by cuihanyu on
 */

public class HomeDetailActivity extends AppCompatActivity implements MVPView {

    private ExpandableListView mExpandableListView;
    private MVPPresent mvpPresent;
    private Intent intent;
    private TextView mTitle;
    private List<HomeEXDetailBean.MiniArrayBean> list;
    private HomeExpandableListViewAdapter adapter;
    private ImageView mBack;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homedetail);
        intent = getIntent();
        initView();
    }

    private void initView() {
        mBack = findViewById(R.id.back_ImageView);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitle = findViewById(R.id.title_tv);
        mExpandableListView = findViewById(R.id.home_ex);
        initData();
    }

    private void initData() {
        mTitle.setText(intent.getStringExtra("Name"));
        mvpPresent = new MVPPresent();
        mvpPresent.attachView(this);
        mvpPresent.requestData(GlobalContent.HOME_Detail);
    }

    @Override
    public void onError(Call call, Exception e, int id) {
        MyLogUtil.e("1111111111onError", e.toString());
    }

    @Override
    public void onResponse(String response, int id) {
        MyLogUtil.e("1111111111onError", response.toString());
        if (list == null) {
            list = new ArrayList<>();
        }
        list.clear();
        HomeEXDetailBean bean = GsonUtil.GsonToBean(response, HomeEXDetailBean.class);
        list.addAll(bean.getMiniArray());
        adapter = new HomeExpandableListViewAdapter(HomeDetailActivity.this, list);
        mExpandableListView.setAdapter(adapter);
        mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                intent.setClass(HomeDetailActivity.this, VideoActivity.class);
                intent.putExtra("Chapter", list.get(groupPosition).getTitle());
                intent.putExtra("item", list.get(groupPosition).getBars().get(childPosition).getTitle());
                startActivity(intent);
                return false;
            }
        });
    }
}
